package cn.gok.service.impl;

import cn.gok.controller.dto.MaintainDormitorySearchDTO;
import cn.gok.dao.MaintainDormitoryDAO;
import cn.gok.entity.MaintainDormitory;
import cn.gok.service.IMaintainDormitoryService;
import cn.gok.util.MybatisUtil;
import cn.gok.util.ResultData;
import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;

import java.util.*;

/**
 * @Description:
 * @Author: chen fan
 * @Date: 2022年10月20日 10:17:11
 */

public class MaintainDormitoryServiceImpl implements IMaintainDormitoryService {
    private SqlSession sqlSession = MybatisUtil.getSQLSession();

    @Override
    public ResultData getMaintainDormitoryList(MaintainDormitorySearchDTO searchDTO) {
        MaintainDormitoryDAO dormitoryDAO = sqlSession.getMapper(MaintainDormitoryDAO.class);

        //开启分页操作
        PageHelper.startPage(searchDTO.getCurrPage(),searchDTO.getPageSize());
        //查询
        List<MaintainDormitory> maintainDormitoryList = dormitoryDAO.findMaintainDormitoryByStuId(searchDTO);

        //通过PageHelper构建出分页信息对象
        PageInfo<MaintainDormitory> pageInfo = PageInfo.of(maintainDormitoryList);

        /*
           前端需要怎样的数据：
                var maintainDormitoryList ={
						result: ture, // 返回状态
						msg: '',		// 返回信息
						pagination: { // 翻页数据
							currPage: 1,// 当前第几页
							total: 100, //数据总数
							pageSize: 10,  // 每页条数
						},
						datas:[
							{'title':'水管损坏','desc':'水管损坏','createTime':2022-12-22,'state':1},
							{'title':'椅子损坏','desc':'椅子损坏','createTime':2022-11-22,'state':2}
						]
					}
         */
        ResultData resultData = new ResultData();
        resultData.put("result",true);
        resultData.put("msg","");

        //构建翻页对象
        Map<String, Object> pagination = new HashMap<>();
        pagination.put("currPage",searchDTO.getCurrPage());
        pagination.put("total",pageInfo.getTotal());
        pagination.put("pageSize",searchDTO.getPageSize());

        //将翻页对象存入结果集中
        resultData.put("pagination",pagination);

        resultData.put("datas",maintainDormitoryList);
        return resultData;
    }
}
